package ru.spb.osll.myfit.client.view;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import ru.spb.osll.myfit.client.event.PageButtonEvent.PageButtonEventType;
import ru.spb.osll.myfit.client.widget.*;
import ru.spb.osll.myfit.client.widget.VerifierImage.VerifierState;

public class MyFitToolBar extends ToolBar {
  private VerifierImage m_verImage;

  public MyFitToolBar(PageButtonEventListener pageButtonEventListener, String pagePath) {
    super(pageButtonEventListener, pagePath);

    createRootWidget();

    createEditToolPanel();

    createViewToolPanel();

    createTitlePanel();
  }

  private Widget createRootWidget() {
    Widget verificationPanel = createVerificationPanel();
    m_mainPanel.add(verificationPanel);

    m_mainPanel.setCellWidth(verificationPanel, "70px");

    m_mainPanel.setCellHorizontalAlignment(verificationPanel, HasHorizontalAlignment.ALIGN_LOCALE_END);

    return m_mainPanel;
  }

  private void createEditToolPanel() {
    m_editToolPanel.add(new PageButton("sendButtonStyle", PageButtonEventType.SAVE_PAGE, m_buttonEventListener));
    m_editToolPanel.add(new PageButton("verifyTestPageStyle", PageButtonEventType.VERIFY_PAGE, m_buttonEventListener));
    HorizontalPanel separator = new HorizontalPanel();
    separator.setWidth("15");
    m_editToolPanel.add(separator);

    m_editToolPanel.add(new PageButton("expandAllIncludesButtonStyle", PageButtonEventType.EXPAND_ALL, m_buttonEventListener));
    m_editToolPanel.add(new PageButton("collapseAllIncludesButtonStyle", PageButtonEventType.COLLAPSE_ALL, m_buttonEventListener));
    m_editToolPanel.add(new PageButton("discardButtonStyle", PageButtonEventType.REFRESH, m_buttonEventListener));
    m_editToolPanel.add(new PageButton("addTableButtonStyle", PageButtonEventType.ADD_TABLE, m_buttonEventListener));
    m_editToolPanel.add(new PageButton("importPageButtonStyle", PageButtonEventType.ADD_INCLUDE, m_buttonEventListener));
    m_editToolPanel.add(new PageButton("textInputButtonStyle", PageButtonEventType.ADD_WIKI, m_buttonEventListener));
    m_editToolPanel.add(new PageButton("addContentButtonStyle", PageButtonEventType.ADD_CONTENT, m_buttonEventListener));
    m_editToolPanel.add(new PageButton("addDefineButtonStyle", PageButtonEventType.ADD_DEFINE, m_buttonEventListener));
    TagAddPanelWidget tagAddPanel = new TagAddPanelWidget();    
    tagAddPanel.setStyleName(IStyleConstants.TAG_ADD_PANEL_STYLE);
    tagAddPanel.setListener(m_buttonEventListener);
    m_editToolPanel.add(tagAddPanel);
  }

  private void createViewToolPanel() {
    m_viewToolPanel.add(new PageButton(IStyleConstants.TEST_BUTTON_STYLE_NAME, PageButtonEventType.TEST_PAGE, m_buttonEventListener));
    m_viewToolPanel.add(new PageButton(IStyleConstants.RUN_SUITE_BUTTON_STYLE_NAME, PageButtonEventType.RUN_SUITE, m_buttonEventListener));
    HorizontalPanel separator = new HorizontalPanel();
    separator.setWidth("15");
    m_viewToolPanel.add(separator);
    m_viewToolPanel.add(new PageButton("expandAllIncludesButtonStyle", PageButtonEventType.EXPAND_ALL, m_buttonEventListener));
    m_viewToolPanel.add(new PageButton("collapseAllIncludesButtonStyle", PageButtonEventType.COLLAPSE_ALL, m_buttonEventListener));
    m_viewToolPanel.add(new PageButton(IStyleConstants.RELOAD_TEMPLATES_BUTTON_STYLE, PageButtonEventType.RELOAD_TABLE_TEMPLATES,
        m_buttonEventListener));
    m_viewToolPanel.add(new PageButton(IStyleConstants.RESULTS_FOR_PAGE_BUTTON_STYLE_NAME, PageButtonEventType.RESULTS_FOR_PAGE,
        m_buttonEventListener));
    
  }

  private void createTitlePanel() {
    m_titlePanel.add(m_titleFormatter.createSeparatedTitle(m_path));
  }

  private Widget createVerificationPanel() {
    Grid verificationPanel = new Grid(1, 1);
    verificationPanel.setStyleName(IStyleConstants.VERIFIER_PANEL_STYLE_NAME);
    m_verImage = new VerifierImage();
    verificationPanel.setWidget(0, 0, m_verImage);
    return verificationPanel;
  }

  public void setVerificationResult(boolean hasError) {
    if (hasError) {
      m_verImage.setState(VerifierState.ERROR);
    } else {
      m_verImage.setState(VerifierState.OK);
    }
  }
}
